package com.tanhua.dubbo.mongo.api;

import com.tanhua.model.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    //今日佳人, 匹配度最高的
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //构建Criteria 查询条件
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建query
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //根据mongoTemplate查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    //推荐朋友 --首页推荐
    public List<RecommendUser> queryRecommendUserlist(Long toUserId, Integer page, Integer pagesize) {
        //构建Criteria 查询条件
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建query
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score"))).skip((page - 1) * pagesize).limit(pagesize);
        //根据mongoTemplate查询
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        return recommendUsers;
    }

    //佳人信息 根据佳人信息查询
    public RecommendUser queryByUserId(Long userId, Long toUserId) {

        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setScore(95d);
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
        }

        return recommendUser;
    }

    @Override
    public Map<Long, RecommendUser> fanCount(List<Long> userLikes, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userLikes).and("toUserId").is(userId);
        Query query = Query.query(criteria);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        Map<Long, RecommendUser> map = new HashMap();
        for (RecommendUser recommendUser : recommendUsers) {
            map.put(recommendUser.getUserId(), recommendUser);
        }
        return map;
    }

    @Override
    public Map<Long, RecommendUser> loveCount(List<Long> userLikes, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(userLikes);
        Query query = Query.query(criteria);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        Map<Long, RecommendUser> map = new HashMap();
        for (RecommendUser recommendUser : recommendUsers) {
            map.put(recommendUser.getUserId(), recommendUser);
        }
        return map;
    }

    @Override
    public Map<Long, RecommendUser> eachLoveCount(List<Long> userLikes, Long userId) {
        Criteria criteria = Criteria.where("userId").in(userLikes).and("toUserId").is(userId)
                /*.and("userId").is(userLikes).and("toUserId").is(userId)*/;
        Query query = Query.query(criteria);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        Map<Long, RecommendUser> map = new HashMap();
        for (RecommendUser recommendUser : recommendUsers) {
            map.put(recommendUser.getUserId(), recommendUser);
        }
        return map;
    }
}

